package cn.tanghom.support.utils;

import cn.tanghom.conf.CommonKey;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
  
    //Redis服务器IP
    private static String ADDR = "127.0.0.1";
    //Redis的端口号
    private static int PORT = 6379;
    
    private static String PASSWORD = null;

    /**
     * 可用连接实例的最大数目，默认值为8；
     * 如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
     */
    private static int MAX_ACTIVE = 1024;
    /**
     * 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
     */
    private static int MAX_IDLE = 200;
    /**
     * 等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
     */
    private static int MAX_WAIT = 10000;
    /**
     * 在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
     */
    private static boolean TEST_ON_BORROW = true;
    
   
    /**
     * 获取Jedis实例
     *
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (pool != null) {
                Jedis resource = pool.getResource();
                return resource;
            } else {
            	INSTANCE = new  RedisUtil();
                return getJedis();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 释放jedis资源
     *
     * @param jedis
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }  

    private static JedisPool pool;
    
    public static RedisUtil INSTANCE = null;
    
    static {
    	INSTANCE = new  RedisUtil();
    }

    private RedisUtil() {    
    	ADDR = CommonKey.jdbc.getProperty("redis.host",ADDR);
    	PORT = CommonKey.jdbc.getInteger("redis.port",PORT);
    	PASSWORD = CommonKey.jdbc.getProperty("redis.password",PASSWORD);
    	MAX_WAIT = CommonKey.jdbc.getInteger("redis.timeout",10000);
	      JedisPoolConfig config = new JedisPoolConfig();
	      config.setMaxTotal(MAX_ACTIVE);
	      config.setMaxIdle(MAX_IDLE);
	      config.setMaxWaitMillis(MAX_WAIT);
	      config.setTestOnBorrow(TEST_ON_BORROW);
	      pool = new JedisPool(config, ADDR, PORT, MAX_WAIT, PASSWORD);          
    }
    
    public void sadd(String key, String value) {
        Jedis jedis = null;
        try{
            jedis = pool.getResource();
            jedis.setex(key, 60 * 60 * 24, value);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public void srem(String key) {
        Jedis jedis = null;
        try{
            jedis = pool.getResource();
            jedis.del(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public boolean sismember(String key) {
        Jedis jedis = null;
        try{
            jedis = pool.getResource();
            return jedis.exists(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    
    public String sget(String key) {
        Jedis jedis = null;
        try{
            jedis = pool.getResource();
            return jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    
    /**
     * 忘记密码记录token使用，1小时内重置密码
     * @param key
     * @param value
     */
    public void saddByPw(String key, String value) {
        Jedis jedis = null;
        try{
            jedis = pool.getResource();
            jedis.setex(key, 60 * 60 * 1, value);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    
    /**
     * 查询key的剩余生存时间
     * @param key
     * @return
     */
    public Long sttl(String key) {
        Jedis jedis = null;
        try{
            jedis = pool.getResource();
            return jedis.ttl(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}